package eu.codlab.mouse_client;


import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.OnScaleGestureListener;

/**
 * Manage the ImageView where the events will be managed
 * @author kevin
 *
 */
public class MouseMoveArea extends ImageView {
	Context _context;
	Client _client = null;
	GestureDetector mGestureDetector;
	private ScaleGestureDetector mScaleDetector;

	public void init(Context context){
		_context = context;
	}
	public MouseMoveArea(Context context) {
		super(context);
		init(context);
	}
	public MouseMoveArea(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	public MouseMoveArea(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}
	public void initGestureDetection() {
		// Gesture detection
		mScaleDetector = new ScaleGestureDetector(_context, new OnScaleGestureListener(){

			@Override
			public boolean onScale(ScaleGestureDetector arg0) {
				if(arg0.getScaleFactor() > 1){
					//send the scroll to the up
					// 0x0, 0x0, 0x0, 0x5,
					// 0x0, 0x0, 0x0, 0x5,
					// 0x0, 0x0, 0x0, 0x5,
					_client = new Client();
					_pos[0] = 5;
					_pos[1] = 5;
					_pos[2] = 5;
					_client.execute(_pos);
				}else{
					//send the scroll to the down
					// 0x0, 0x0, 0x0, 0x6,
					// 0x0, 0x0, 0x0, 0x6,
					// 0x0, 0x0, 0x0, 0x6,
					_client = new Client();
					_pos[0] = 6;
					_pos[1] = 6;
					_pos[2] = 6;
					_client.execute(_pos);
					_client = null;
				}
				waiting = 20;
				return true;
			}

			@Override
			public boolean onScaleBegin(ScaleGestureDetector arg0) {
				return true;
			}

			@Override
			public void onScaleEnd(ScaleGestureDetector arg0) {
			}
		});
		mGestureDetector = new GestureDetector(new OnGestureListener() {
			@Override
			public boolean onDown(MotionEvent arg0) {
				return false;
			}

			@Override
			public boolean onFling(MotionEvent e1, MotionEvent e2,
					float velocityX, float velocityY) {
				return false;
			}

			@Override
			public void onLongPress(MotionEvent e) {
			}

			@Override
			public boolean onScroll(MotionEvent e1, MotionEvent e2,
					float distanceX, float distanceY) {
				return false;
			}

			@Override
			public void onShowPress(MotionEvent e) {
			}

			@Override
			public boolean onSingleTapUp(MotionEvent e) {
				//send the tap event
				// 0x0, 0x0, 0x0, 0x4,
				// 0x0, 0x0, 0x0, 0x4,
				// 0x0, 0x0, 0x0, 0x4,
				_client = new Client();
				_pos[0] = 4;
				_pos[1] = 4;
				_pos[2] = 4;
				_client.execute(_pos);
				_client = null;
				waiting = 0;

				return true;
			}
		});
	}
	Integer _pos[]=new Integer[3];
	int tot=0;
	
	private void move(MotionEvent event){
		tot++;
		if(tot>20){
			//send the down event
			// 0x0, 0x0, 0x0, 0x2,
			// x
			// y
			_client = new Client();
			_pos[0] = 2;
			_pos[1] = (int)event.getX();
			_pos[2] = (int)event.getY();
			//Log.d("move", " "+_pos[0]);
			_client.execute(_pos);
			_client = null;
		}
	}
	
	/**
	 * A finger touched the screen
	 * @param event the MotionEvent to parse
	 */
	private void down(MotionEvent event){
		//send the down event
		// 0x0, 0x0, 0x0, 0x3,
		// x
		// y
		_client = new Client();
		_pos[0] = 3;
		_pos[1] = (int)event.getX();
		_pos[2] = (int)event.getY();
		_client.execute(_pos);
		_client = null;
	}
	
	int waiting=0;
	
	/**
	 * Manage onTouchEvent on the screen
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(mGestureDetector == null)
			initGestureDetection();
		
		//first of all if there are lots of finger (at least 2 :p )
		//we return if there is a pinch
		//a scroll event in this case
		if(event.getPointerCount() > 1 && mScaleDetector.onTouchEvent(event))
			return true;
		
		//then if it there is a tap
		//we return
		if(mGestureDetector.onTouchEvent(event))
			return true;
		
		//if(waiting > 0)
		//	return true;
		
		//if it is a move event
		if(event.getAction() == MotionEvent.ACTION_MOVE){
			move(event);
		//or a up
		//note that ACTION_UP is buggy :s
		}else if(event.getAction() == MotionEvent.ACTION_UP){
			waiting = 0;
		//or the screen has just been touched
		}else if(event.getAction() == MotionEvent.ACTION_DOWN){
			down(event);
		}else{
			//mouahahahahahahahahahahahahah
		}
		return true;
	}
}
